﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PCMSofCC.BLL;
using PCMSofCC;

namespace PCMSofCC.Admin
{
    public partial class ModifyScoreChart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            List<int> entries = new List<int>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cbx = (CheckBox)row.FindControl("CheckBox1");
                if (cbx.Checked)
                {
                    entries.Add((int)GridView1.DataKeys[row.RowIndex].Value);
                }
            }
            int chartId = int.Parse(SelectScoreChart.SelectedValue);
            Charts.DeleteEntries(chartId);
            Charts.AddEntries(chartId, entries);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSelectChart_Click(object sender, EventArgs e)
        {
            int chartId = int.Parse(SelectScoreChart.SelectedValue);
            var allEntries = ScoreEntries.GetAllEntries();
            var chartEntries = ScoreEntries.GetChartEntries(chartId);
            GridView1.DataSource = allEntries;
            GridView1.DataBind();
            foreach (GridViewRow row in GridView1.Rows)
            {
                int entryId = (int)(GridView1.DataKeys[row.RowIndex].Value);
                if (chartEntries.Any(x => x.Id == entryId))
                {
                    ((CheckBox)row.FindControl("CheckBox1")).Checked = true;
                }
                else
                {
                    ((CheckBox)row.FindControl("CheckBox1")).Checked = false;
                }
            }
        }
    }
}
